/**
 * 酒精灯
 */
Lamp = Widget.extend({
	ctor:function(parent){
		this._super();
		parent.addChild(this,21);
		this.setTag(TAG_LAMP);
		this.init();
	},
	init : function(){
		this.setVisible(false);
		this.setCascadeOpacityEnabled(true);
		//酒精灯瓶身(左边)
		var bottle1 = new Button(this, 10, TAG_LAMP_BOTTLE1, "#run/lampBottle.png",this.callback);
		bottle1.setPosition(cc.p(451,185));
		bottle1.setScale(0.6);
		//酒精灯灯帽(左边)
		var lid1 = new Button(bottle1, 11, TAG_LAMP_LID1, "#run/lampLid.png",this.callback,this);
		lid1.setPosition(cc.p(101,223));
		//酒精灯瓶身(右边)
		var bottle2 = new Button(this, 10, TAG_LAMP_BOTTLE2, "#run/lampBottle.png",this.callback);
		bottle2.setPosition(cc.p(883,185));
		bottle2.setScale(0.6);
		//酒精灯灯帽(右边)
		var lid2 = new Button(bottle2, 11, TAG_LAMP_LID2, "#run/lampLid.png",this.callback,this);
		lid2.setPosition(cc.p(101,223));
		//火柴
		var match = new Button(this, 11, TAG_LAMP_MATCH, "#run/match.png",this.callback);
		match.setPosition(cc.p(660,130));
		match.setScale(0.12);
	},	
	fire:function(p){
		var fire = new cc.Sprite("#fire/fire1.png");
		p.addChild(fire,3,TAG_FIRE);
		fire.setPosition(cc.p(99,273));
		fire.setScale(2.5);

		var animFrames = [];
		for (var i = 1; i < 4; i++) {
			var str = "fire/fire" + i + ".png";
			var frame = cc.spriteFrameCache.getSpriteFrame(str);
			animFrames.push(frame);
		}
		var animation = new cc.Animation(animFrames, 0.05);
		var action = cc.animate(animation);
		var repeat = cc.repeatForever(action);
		fire.runAction(repeat);
	},
	callback:function(p){
		var action = gg.flow.flow.action;
		switch(p.getTag()){
		case TAG_LAMP_BOTTLE1:
			if(action  == ACTION_DO1){
				var mo1 = cc.moveTo(1, cc.p(530,166));
				var se = cc.sequence(mo1,cc.callFunc(function() {
					//灯帽
					var lid = p.getChildByTag(TAG_LAMP_LID1);
					var move = cc.moveTo(0.5, cc.p(102,244));
					var bezier = cc.bezierBy(1,[cc.p(-120,120),cc.p(-250,-80),cc.p(-250,-170)]);
					var sequence = cc.sequence(move,bezier,cc.callFunc(function() {
						lid.removeFromParent(true);
						this.addChild(lid);
						lid.setScale(0.6);
						lid.setPosition(cc.p(381,140));
					}, this));
					lid.runAction(sequence);
				}, this),cc.delayTime(1.6),cc.callFunc(function() {
					//单根火柴
					var match = this.getChildByTag(TAG_LAMP_MATCH);
					var match1 = new cc.Sprite("#run/match2.png");
					match.addChild(match1, 2);
					match1.setScale(2);
					match1.setPosition(cc.p(658,617));
					match1.runAction(cc.sequence(cc.moveTo(0.5, cc.p(968,453)),cc.callFunc(function() {
						//火柴点燃
						match1.setSpriteFrame("run/match1.png");
					}, this),cc.moveTo(0.5, cc.p(984, 515)),cc.moveTo(1, cc.p(-355,1240)),cc.callFunc(function() {
						//酒精灯点燃
						this.fire(p);	
					}, this),cc.fadeOut(0.1)));							
				}, this),cc.delayTime(2.1),cc.callFunc(function() {
					gg.flow.next();
				}, this));
				p.runAction(se);	
			}else if(action == ACTION_DO2){
				//左边酒精灯归位
				var move1 = cc.moveTo(1, cc.p(451,185));
				p.runAction(cc.sequence(move1, cc.delayTime(0.5), cc.callFunc(function() {
					this.schedule(ll.run.beaker.beakerBubble, 1);
					this.scheduleOnce(ll.run.temp.tempBuddle, 1.5);
				}, this)));
				//右边酒精灯归位
				var lamp_right = this.getChildByTag(TAG_LAMP_BOTTLE2);
				var move2 = cc.moveTo(1, cc.p(883,185));
				lamp_right.runAction(cc.sequence(move2,cc.callFunc(function() {
					var show = new ShowTip("观察温度变化",cc.p(570,240),true);
				}, this),cc.delayTime(1),cc.callFunc(function() {
					this.action_left = cc.scaleTo(35, 4, 4);
					this.action_right = cc.scaleTo(15, 4, 4);
					//温度计变化（左）
					var line1 = ll.run.getChildByTag(TAG_TEMP).getChildByTag(TAG_TEMP_SP1).getChildByTag(TAG_TEMP_LINE1);
					line1.runAction(this.action_left);
					//温度计变化（右）
					var line2 = ll.run.getChildByTag(TAG_TEMP).getChildByTag(TAG_TEMP_SP2).getChildByTag(TAG_TEMP_LINE2);
					line2.runAction(this.action_right);
				}, this),cc.delayTime(4),cc.callFunc(function() {
					gg.flow.next();
				}, this)));
			}else if(action == ACTION_DO3){
				var move = cc.moveTo(1, cc.p(680,120));
				var sequ = cc.sequence(cc.callFunc(function() {
					//温度计（左）停止上升
					var line1 = ll.run.getChildByTag(TAG_TEMP).getChildByTag(TAG_TEMP_SP1).getChildByTag(TAG_TEMP_LINE1);
					line1.stopAction(this.action_left);
					//烧杯内停止气泡
					this.unschedule(ll.run.beaker.beakerBubble);
				}, this), move, cc.callFunc(function() {
					//灯帽
					var lid = this.getChildByTag(TAG_LAMP_LID1);
					var mov = cc.moveTo(1, cc.p(634,205));
					var rot = cc.rotateTo(1, -35);
					var spa = cc.spawn(mov,rot);
					var mov1 = cc.moveTo(0.3, cc.p(663,205));
					var mov2 = cc.moveTo(0.5, cc.p(679,184));
					var rot2 = cc.rotateTo(0.5, 0);
					var spa2 = cc.spawn(mov2,rot2);
					var mov3 = cc.moveBy(1,0,30);
					var mov4 = cc.moveBy(1,0,-30);
					var seq = cc.sequence(spa, mov1,cc.callFunc(function() {
						//火焰熄灭
						var fire = this.getChildByTag(TAG_LAMP_BOTTLE1).getChildByTag(TAG_FIRE);
						fire.removeFromParent();
					}, this),spa2,cc.delayTime(0.5),mov3,cc.delayTime(0.3),mov4, cc.callFunc(function() {
						lid.removeFromParent(true);
						p.addChild(lid,5,TAG_LAMP_LID1);
						lid.setScale(1);
						lid.setPosition(cc.p(100,223));
					}, this));
					lid.runAction(seq);
				}, this),cc.delayTime(5),cc.fadeOut(0),cc.callFunc(function() {
					var lid = p.getChildByTag(TAG_LAMP_LID1);
					lid.runAction(cc.fadeOut(0));
					var show = new ShowTip("质量相同的不同物质，吸收的热量不相同；\n实验证明：水的比热比砂石大。",cc.p(405,220));
				}, this),cc.delayTime(5),cc.callFunc(function() {
					gg.flow.next();
					this.removeFromParent(true);
				}, this));
				p.runAction(sequ);
			}
			break;
		case TAG_LAMP_BOTTLE2:
			if(action  == ACTION_DO1){
				var mo1 = cc.moveTo(1, cc.p(810,167));
				var se = cc.sequence(mo1,cc.callFunc(function() {
					//灯帽
					var lid = p.getChildByTag(TAG_LAMP_LID2);
					var move = cc.moveTo(0.5, cc.p(102,244));
					var bezier = cc.bezierBy(1,[cc.p(120,120),cc.p(250,-80),cc.p(250,-170)]);
					var sequence = cc.sequence(move,bezier,cc.callFunc(function() {
						lid.removeFromParent(true);
						this.addChild(lid);
						lid.setScale(0.6);
						lid.setPosition(cc.p(961,141));
					}, this));
					lid.runAction(sequence);
				}, this),cc.delayTime(1.6),cc.callFunc(function() {
					//单根火柴
					var match = this.getChildByTag(TAG_LAMP_MATCH);
					var match1 = new cc.Sprite("#run/match2.png");
					match.addChild(match1, 2);
					match1.setScale(2);
					match1.setPosition(cc.p(658,617));
					match1.runAction(cc.sequence(cc.moveTo(0.5, cc.p(968,453)),cc.callFunc(function() {
						//火柴点燃
						match1.setSpriteFrame("run/match1.png");
					}, this),cc.moveTo(0.5, cc.p(984, 515)),cc.moveTo(1, cc.p(1900,1240)),cc.callFunc(function() {
						//酒精灯点燃
						this.fire(p);	
					}, this),cc.fadeOut(0.1),cc.callFunc(function() {
						match.removeFromParent(true);
					}, this)));							
				}, this),cc.delayTime(2.1),cc.callFunc(function() {
					var show = new ShowTip("同时加热\n 并观察",cc.p(594,240));
					gg.flow.next();
				}, this));
				p.runAction(se);	
			}else if(action == ACTION_DO2){
				var move = cc.moveTo(1, cc.p(680,120));
				var sequ = cc.sequence(cc.callFunc(function() {
					//温度计（右）停止上升
					var line2 = ll.run.getChildByTag(TAG_TEMP).getChildByTag(TAG_TEMP_SP2).getChildByTag(TAG_TEMP_LINE2);
					line2.stopAction(this.action_right);
				}, this), move, cc.callFunc(function() {
					//灯帽
					var lid = this.getChildByTag(TAG_LAMP_LID2);
					var mov = cc.moveTo(1, cc.p(723,205));
					var rot = cc.rotateTo(1, 35);
					var spa = cc.spawn(mov,rot);
					var mov1 = cc.moveTo(0.3, cc.p(698,205));
					var mov2 = cc.moveTo(0.5, cc.p(680,184));
					var rot2 = cc.rotateTo(0.5, 0);
					var spa2 = cc.spawn(mov2,rot2);
					var mov3 = cc.moveBy(1,0,30);
					var mov4 = cc.moveBy(1,0,-30);
					var seq = cc.sequence(spa, mov1,cc.callFunc(function() {
						//火焰熄灭
						var fire = this.getChildByTag(TAG_LAMP_BOTTLE2).getChildByTag(TAG_FIRE);
						fire.removeFromParent();
					}, this),spa2,cc.delayTime(0.5),mov3,cc.delayTime(0.3),mov4, cc.callFunc(function() {
						lid.removeFromParent(true);
						p.addChild(lid);
						lid.setScale(1);
						lid.setPosition(cc.p(100,223));
					}, this));
					lid.runAction(seq);
				}, this),cc.delayTime(4.8),cc.callFunc(function() {
					var show = new ShowTip("如果水升高到相同的温度,则\n需要加热更长时间。",cc.p(555,220));
					gg.flow.next();
					p.removeFromParent(true);
				}, this));
				p.runAction(sequ);
			}
			break;
		default:
			break;
		}
	}
});
